<template>
  <el-container class="home_container">
    <el-header class="home_header">
      <div class="logo">
        <img src="../assets/images/logo.png" alt />
        <span>商城管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 菜单 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <!-- 折叠菜单 -->
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="(item,index) in menuList" :key="item.id">
            <template slot="title">
              <i class="iconfont" :class="icons[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      icons: [
        "icon-user",
        "icon-tijikongjian",
        "icon-shangpin",
        "icon-danju",
        "icon-baobiao",
        "icon-lock_fill",
      ],
      iscollapse: false,
    };
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取菜单列表
    getMenuList() {
      this.$http({
        url: "menus",
      }).then((res) => {
        this.menuList = res.data.data;
      });
    },
    // 折叠菜单
    toggleCollapse() {
      this.iscollapse = !this.iscollapse;
    },
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style lang="less">
.home_container {
  height: 100%;
  background: #363d40;
  .el-header {
    background: #363d40;
    padding-left: 0 !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-aside {
    background-color: rgb(84, 92, 100);
    .toggle_button {
      background: #475163;
      font-size: 12px;
      color: #fff;
      text-align: center;
      cursor: pointer;
      height: 26px;
      line-height: 26px;
      // 字体间距
      letter-spacing: 0.2em;
    }
  }
  .el-main {
    background: #e9edf0;
  }
  .el-menu {
    border: none;
  }
  .logo {
    display: flex;
    height: 100%;
    align-items: center;
  }
  .logo span {
    font-size: 22px;
    color: #fff;
    margin-left: 20px;
  }
  .logo img {
    height: 100%;
  }
  .iconfont {
    margin-right: 10px;
  }
}
</style>